﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using System.IO;
using System.Security.Cryptography;
using DevExpress.XtraReports.UI;
using DrugsShopManagement.DBCLASS;
using System.Data.SqlClient;

namespace DrugsShopManagement
{
    public partial class FrmDrugsSalesDetail : DevExpress.XtraEditors.XtraForm
    {
        public FrmDrugsSalesDetail()
        {
            InitializeComponent();
        }
        Operator op = new Operator();

        private void FrmDrugsSalesDetail_Load(object sender, EventArgs e)
        {
            
            //DataTable dt1 = new DataTable();
            //string sql = "select drugsSalesId 药品销售号,drugsId 药品号,drugsName 药品名称,drugsPY 拼音码,drugsSpecification 药品规格,drugsUnit 计量单位,drugsSalePrice 销售单价,drugsSalesNum 销售数量,drugsSalesMoney 销售金额,drugsSalesTime 销售时间,drugsFactory 生产厂家,drugsUsefulDate 有效期,drugsSalesMan 销售员 from drugsSales order by drugsSalesId";
            //dt1 = op.getDataTable(sql, "drugsSales");
            //dataGridView2.DataSource = dt1;

            DataTable dt2 = new DataTable();
            string sql2 = "select drugsSalesId 药品销售号,drugsId 药品号,drugsName 药品名称,drugsPY 拼音码,drugsSpecification 药品规格,drugsUnit 计量单位,drugsSalePrice 销售单价,drugsSalesNum 销售数量,drugsSalesMoney 销售金额,drugsSalesTime 销售时间,drugsFactory 生产厂家,drugsUsefulDate 有效期,drugsOutID 出库单号,drugsSalesMan 销售员 from drugsUnSales order by drugsSalesId";
            dt2 = op.getDataTable(sql2, "drugsUnSales");
            dataGridView1.DataSource = dt2;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                XtraReport report = new XtraReport();
                string path = System.IO.Directory.GetCurrentDirectory();
                report.LoadLayout(path + "..\\..\\..\\Reports\\销售单报表.repx");
                ReportPrintTool printTool = new ReportPrintTool(report);
                //DataTable dt = new DataTable();
                DataSet ds = new DataSet();   
                string sql = "select *,getdate() from drugsUnSales";
                ds = op.getDataSet(sql);
                report.DataSource = ds;
                printTool.ShowRibbonPreview();
            }
            catch (Exception ex)
            {
                MessageBox.Show("无法打开报表，详细信息：" + ex.ToString());
            }   
    
            using (SqlConnection con = new SqlConnection(getSqlConnection.conStr))
            {
                con.Open();
                using (SqlCommand cmd = con.CreateCommand())
                {
                    cmd.CommandText = "delete from drugsUnSales";
                    cmd.ExecuteNonQuery();//会返回受影响的行数 
                }
            }
        }

        private void btnconcel_Click(object sender, EventArgs e)
        {
            try
            {
                if (this.textBox1.Text == "")
                {
                    MessageBox.Show("请选择需要删除的内容！");
                    return;
                }
                if (MessageBox.Show("确定需要删除药品[ " + this.textBox2.Text.Trim() + " ]吗？", "提示", MessageBoxButtons.OKCancel) == DialogResult.OK)
                {
                    op.ExecuteSql("delete from drugsUnSales where drugsSalesId='" + textBox1.Text.Trim() + "'");
                    op.ExecuteSql("delete from drugsSales where drugsSalesId='" + textBox1.Text.Trim() + "'");
                    //DataTable dt1 = new DataTable();
                    //string sql = "select drugsSalesId 药品销售号,drugsId 药品号,drugsName 药品名称,drugsPY 拼音码,drugsSpecification 药品规格,drugsUnit 计量单位,drugsSalePrice 销售单价,drugsSalesNum 销售数量,drugsSalesMoney 销售金额,drugsSalesTime 销售时间,drugsFactory 生产厂家,drugsUsefulDate 有效期,drugsOutID 出库ID,drugsSalesMan 销售员 from drugsSales order by drugsSalesId";
                    //dt1 = op.getDataTable(sql, "drugsSales");
                    //dataGridView2.DataSource = dt1;

                    //DataTable dr = new DataTable();
                    //dr = op.getDataTable("select * from drugsUnSales where drugsSalesId='" + textBox1.Text.Trim() + "'", "drugsUnSales");
                    //string id = dr.Rows[0]["drugsOutID"].ToString();
                    //DataTable dr1 = new DataTable();
                    //dr1 = op.getDataTable("select * from drugsOut where drugsId='" + id + "'", "drugsOut");
                    //string  outno = dr1.Rows[0]["drugsOutNo"].ToString();
                    //int o= Convert.ToInt32(outno);
                    //int num=Convert.ToInt32 (textBox3.Text);
                    //int ad = o + num;
                    //DataTable dt3 = new DataTable();
                    //string sql = "update drugsOut set drugsOutNo=' " + ad  + "' where drugsOutID= '" + id + "'";
                    //dt3 = op.getDataTable(sql, "drugsOut");

                    DataTable dt2 = new DataTable();
                    string sql2 = "select drugsSalesId 药品销售号,drugsId 药品号,drugsName 药品名称,drugsPY 拼音码,drugsSpecification 药品规格,drugsUnit 计量单位,drugsSalePrice 销售单价,drugsSalesNum 销售数量,drugsSalesMoney 销售金额,drugsSalesTime 销售时间,drugsFactory 生产厂家,drugsUsefulDate 有效期,drugsOutID 出库ID,drugsSalesMan 销售员 from drugsUnSales order by drugsSalesId";
                    dt2 = op.getDataTable(sql2, "drugsUnSales");
                    dataGridView1.DataSource = dt2;
                    MessageBox.Show("删除成功！", "温馨提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    this.textBox1.Text = "";
                }
               

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                return;
            }        
            
        }

        private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            try
            {
                DataTable dt_1 = new DataTable();
                dt_1 = op.getDataTable("select * from drugsUnSales where drugsSalesId='" + dataGridView1.CurrentRow.Cells[0].Value.ToString() + "'", "drugsUnSales");
                this.textBox1.Text = dt_1.Rows[0]["drugsSalesId"].ToString();
                this.textBox2.Text = dt_1.Rows[0]["drugsName"].ToString();
                this.textBox3.Text = dt_1.Rows[0]["drugsSalesNum"].ToString();

            }
            catch (Exception ex) { MessageBox.Show(ex.Message); return; }
        }
    }
}